package mobi.pooper;

import static mobi.pooper.Constants.PICTURE_HEIGHT;
import static mobi.pooper.Constants.PICTURE_WIDTH;
import static mobi.pooper.Constants.TASKS_NUMBER;
import static mobi.pooper.Constants.TASKS_PATH;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;

import mobi.pooper.db.IPicsDbAdapter;
import mobi.pooper.pictures.Picture;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.utils.Scaling;

import static mobi.pooper.Constants.*;

public class GameScreen implements Screen {

	protected static final String TAG = GameScreen.class.getSimpleName();

	private Pooper pooper;
	private GameStage stage;
	
	private int rightVariant;
	private BitmapFont taskTextFont;
	
	public GameScreen(Pooper pooper) throws IOException {
		this.pooper = pooper;
		
		Random rnd = new Random(System.currentTimeMillis());
		rightVariant = rnd.nextInt(TASKS_NUMBER);

		stage = new GameStage(pooper, NATIVE_WIDTH, NATIVE_HEIGHT, false);
		
		Set<Picture> pics = getVariantPictures(rnd);
		
		taskTextFont = new BitmapFont(Gdx.files.internal("data/fonts/rus.fnt"), false);
		CharSequence taskText = getTaskText(pics, rightVariant);
		Label lbl = new Label(taskText, new Label.LabelStyle(taskTextFont, Color.WHITE));
		lbl.x = NATIVE_WIDTH / 2 - taskTextFont.getBounds(taskText).width / 2;
		lbl.y = NATIVE_HEIGHT * 3 / 4;
		stage.addActor(lbl);
		
		placeImages(pics);
		
		pooper.setRightVariant(pics, rightVariant);

	}

	private CharSequence getTaskText(Set<Picture> pics, int rightVariant) throws IOException {
		
		Picture[] pictures = new Picture[pics.size()];
		pics.toArray(pictures);
		
		FileHandle taskFile = Gdx.files.internal(TASKS_PATH + "/" + pictures[rightVariant].getName() + ".txt");
		BufferedReader br = new BufferedReader(taskFile.reader());
		CharSequence retval = br.readLine();
		
		Gdx.app.debug(TAG, "Right variant = " + retval);
		return retval;
	}

	private Set<Picture> getVariantPictures(Random rnd) {
		IPicsDbAdapter dba = ((IPooperApplication)Gdx.app).getPicsDbAdapter();
		dba.open();
		Picture[] allItems = dba.getAllItems();
		dba.close();
		
		Set<Picture> retval = new HashSet<Picture>();
		
		for(int i = 0; i < TASKS_NUMBER; i++){
			while(retval.add(allItems[rnd.nextInt(allItems.length)]) == false);
		}
		return retval;
	}

	private void placeImages(Set<Picture> pics){

		Iterator<Picture> it = pics.iterator();
		for(int i = 0; i < pics.size(); i++){
			Picture pic = it.next();
			String name = pic.getName();
			Texture texture = new Texture(Gdx.files.internal(TASKS_PATH + "/" + name + ".jpg"));

			float xStep = NATIVE_WIDTH / (TASKS_NUMBER);
			float x = xStep/2 + i * xStep - PICTURE_WIDTH / 2;
			float y = NATIVE_HEIGHT / 4;
			
			Image img = new PickableImage(texture, Scaling.fit, x, y, name, rightVariant == i);
			img.originX = PICTURE_WIDTH / 2;
			img.originY = PICTURE_HEIGHT / 2;
			img.width = PICTURE_WIDTH;
			img.height = PICTURE_HEIGHT;
			
			stage.addActor(img);
			Gdx.input.setInputProcessor(stage);
		}
	}

	@Override
	public void dispose() {
		stage.dispose();
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void render(float arg0) {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		stage.act(Gdx.graphics.getDeltaTime());
		stage.draw();
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}
}
